package cn.com.pkas.net
{
	import cn.com.pkas.bean.Body;
	import cn.com.pkas.bean.Message;
	import cn.com.pkas.comm.Const;
	import cn.com.pkas.comm.Str;
	import cn.com.pkas.service.BussnessProvider;
	import cn.com.pkas.service.impl.FormatMSGImpl;
	import cn.com.pkas.view.V_Tip;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	
	import mx.collections.ArrayCollection;
	
    /**
    *
    * 连接类，每次使用一个连接，使用完毕，自动关闭
    * 发生异常，可以手动关闭
    * 
    * 目前还未做异常处理
    * 
    * */
	public class Connection extends EventDispatcher
	{
		private var _socket:Socket;
		private var _bp:BussnessProvider;
		private var _bussCode:int;
		private var _objs:ArrayCollection;
		private var _msg:Message;
		private var _body:Body;
		private var _ROOT:Object;
		private var _hostRetCode:String;
		private var _hostMessage:String;
		
		public function Connection(_bp:BussnessProvider,_bussCode:int,_objs:ArrayCollection)
		{
			this._socket = new Socket();
			this._bp = _bp;
			this._bussCode = _bussCode;
			this._objs = _objs;
			init();
		}
		
		public function init():void{
			this._socket = new Socket();
			
			this._socket.addEventListener(Event.CONNECT,send);
			this._socket.addEventListener(ProgressEvent.SOCKET_DATA,receive);
			this._socket.addEventListener(IOErrorEvent.IO_ERROR,handler_IO_error);
			this._socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,handler_SE_error);
		}
		
		private function handler_IO_error(ioEvent:IOErrorEvent):void{
			V_Tip.error(Str.STR_12);
			this.dispatchEvent(new ConnEvent(ConnEvent.ERROR));
		}
		
		private function handler_SE_error(sEvent:SecurityErrorEvent):void{
			V_Tip.error(Str.STR_13);
			this.dispatchEvent(new ConnEvent(ConnEvent.SECURITYERROR));
		}

		public function start():void{
		
			this._socket.connect(Const.HOSTNAME,Const.HOSTPORT);

			
		}
		
		public function close():void{
//			Alert.show("执行了关闭");
			this.socket.close();
		}
		
		public function send(e:Event):void{
			bp.doBussness(this.bussCode,this.objs,this.socket);
		}
		
		public function receive(e:ProgressEvent):void{
			var temp:String="";
			while (this.socket.bytesAvailable)
			{
				temp+=this.socket.readMultiByte(socket.bytesAvailable, "utf8");
			}
			
			this.msg =  new FormatMSGImpl().parse(temp);
			this.ROOT = this.msg.body.ROOT;
			this.body = this.msg.body;
			
			this.dispatchEvent(new ConnEvent(ConnEvent.RECEIVED));
			close();
//			Alert.show(this.msg.body.hostMessage);
		}
		
		public function get socket():Socket{
			return this._socket;
		}
		
		public function set socket(socket:Socket):void{
			this._socket = socket;
		}
		
		
		public function get bp():BussnessProvider{
			return this._bp;
		}
		
		public function set bp(bp:BussnessProvider):void{
			this._bp = bp;
		}
		
		public function get bussCode():int{
			return this._bussCode;
		}
		
		public function set bussCode(bussCode:int):void{
			this._bussCode = bussCode;
		}
		
		public function get objs():ArrayCollection{
			return this._objs;
		}
		
		public function set objs(objs:ArrayCollection):void{
			this._objs = objs;
		}
		
		public function get msg():Message{
			return this._msg;
		}
		
		public function set msg(msg:Message):void{
			this._msg = msg;
		}
		
		public function get body():Body{
			return this._body;
		}
		
		public function set body(body:Body):void{
			this._body = body;
		}
		
		public function get ROOT():Object{
			return this._ROOT;
		}
		
		public function set ROOT(ROOT:Object):void{
			this._ROOT = ROOT;
		}
		
		public function get hostRetCode():String{
			return this.ROOT[Const.HOSTRETCODE];
//			return JSON.decode(this._body).root.hostRetCode;
		}
		
		public function set hostRetCode(hostRetCode:String):void{
			this._hostRetCode = hostRetCode;
		}
		
		public function get hostMessage():String{
			return this.ROOT[Const.HOSTMESSAGE];
//			return JSON.decode(this._body).root.hostMessage;
		}
		
		public function set hostMessage(hostMessage:String):void{
			this._hostMessage = hostMessage;
		}

	}
}